package com.ql.gamelogm.dao.impl;


import com.ql.gamelogm.dao.ManagerDao;
import com.ql.gamelogm.entities.Manager;
import com.ql.gamelogm.entities.Right;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ManagerDaoImpl implements ManagerDao {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Manager> findAllManager() {
        String sql = "SELECT * FROM manager";
        List<Manager> managerList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Manager(
                resultSet.getString("id"),
                resultSet.getString("ManagerType"),
                resultSet.getString("name"),
                resultSet.getString("sex"),
                resultSet.getInt("age"),
                resultSet.getString("password"),
                resultSet.getString("location"),
                resultSet.getString("email")
        ));
        return managerList;
    }

    @Override
    public List<Manager> findManagerByID(String id) {
        String sql = "SELECT * FROM manager where id=?";
        List<Manager> managerList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Manager(
                resultSet.getString("id"), resultSet.getString("password")
        ), id);
        return managerList;
    }

    @Override
    public boolean updatePwd(String pwd,String id) {
        String sql="update manager set password='"+pwd+"' where id='"+id+"'";
        int update = jdbcTemplate.update(sql);
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updatenPwd(String pwd, String email) {
        String sql="update manager set password='"+pwd+"' where email='"+email+"'";
        int update = jdbcTemplate.update(sql);
        if(update==0){
            return false;
        }
        return true;
    }


    @Override
    public boolean addManager(Manager manager) {
        String sql="insert into manager values(?,?,?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql,
                manager.getId(),
                manager.getManagerType(),
                manager.getName(),
                manager.getSex(),
                manager.getAge(),
                manager.getPassword(),
                manager.getLocation(),
                manager.getEmail()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateManager(Manager manager,String id) {
        String sql="update manager set name=?,sex=?,age=?,location=?,email=? where id='"+id+"'";
        int update = jdbcTemplate.update(sql,
                manager.getName(),
                manager.getSex(),
                manager.getAge(),
                manager.getLocation(),
                manager.getEmail()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteManager(String id) {
        String sql="delete from manager where id='"+id+"'";
        int update = jdbcTemplate.update(sql);
        if(update==0){
            return false;
        }
        return true;
    }



    @Override
    public List<Right> findAllManagerRight() {
        String sql = "select * from rightsmanage ";
        List<Right> RightList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Right(
                resultSet.getString("id"),
                resultSet.getString("managerType"),
                resultSet.getInt("isMailEdit"),
                resultSet.getInt("isLogEdit"),
                resultSet.getInt("isRightEdit"),
                resultSet.getInt("isPersRightEdit"),
                resultSet.getInt("isCalendarEdit")
        ));
        return RightList;
    }

    @Override
    public List<Right> findRightById(String id) {
        String sql = "select * from rightsmanage where id='"+id+"'";
        List<Right> RightList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new Right(
                resultSet.getString("id"),
                resultSet.getString("managerType"),
                resultSet.getInt("isMailEdit"),
                resultSet.getInt("isLogEdit"),
                resultSet.getInt("isRightEdit"),
                resultSet.getInt("isPersRightEdit"),
                resultSet.getInt("isCalendarEdit")
        ));
        return RightList;
    }

    @Override
    public boolean addRight(Right right) {
        String sql="insert into rightsmanage values(?,?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql,
                right.getId(),
                right.getManagerType(),
                right.getIsMailEdit(),
                right.getIsLogEdit(),
                right.getIsRightEdit(),
                right.getIsPersRightEdit(),
                right.getIsCalendarEdit()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateRight(Right right, String id) {
        String sql="update rightsmanage set isMailEdit=?,isLogEdit=?,isRightEdit=?,isPersRightEdit=?,isCalendarEdit=? where id='"+id+"'";
        int update = jdbcTemplate.update(sql,
                right.getIsMailEdit(),
                right.getIsLogEdit(),
                right.getIsRightEdit(),
                right.getIsPersRightEdit(),
                right.getIsCalendarEdit()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteRightById(String id) {
        String sql="delete from rightsmanage where id='"+id+"'";
        int update = jdbcTemplate.update(sql);
        if(update==0){
            return false;
        }
        return true;
    }


}

